#include <iostream>
#include <vector>
#include <queue>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <functional>
#include <chrono>

struct TaskRecord {
    int taskId;
    int priority;
    std::chrono::steady_clock::time_point submitted;
    std::chrono::steady_clock::time_point started;
    std::chrono::steady_clock::time_point finished;
    std::string status;
};

class TaskHistory {
private:
    std::vector<TaskRecord> records;
    mutable std::shared_mutex mutex;

public:
    void addRecord(const TaskRecord& record) {
        std::lock_guard<std::shared_mutex> lock(mutex);
        records.push_back(record);
    }

    std::vector<TaskRecord> getRecords() const {
        std::shared_lock<std::shared_mutex> lock(mutex);
        return records;
    }
};

class ThreadPool {
private:
    TaskHistory history;
    int taskIdCounter = 0;
    std::mutex taskMutex;

public:
    void executeTask(int priority, std::function<void()> func) {
        std::lock_guard<std::mutex> lock(taskMutex);
        auto submittedTime = std::chrono::steady_clock::now();
        TaskRecord record{taskIdCounter++, priority, submittedTime, {}, {}, "Submitted"};
        history.addRecord(record);

        // Update task record on start
        record.started = std::chrono::steady_clock::now();
        record.status = "Running";

        func();  // Execute the task

        // Update task record on completion
        record.finished = std::chrono::steady_clock::now();
        record.status = "Completed";
        history.addRecord(record);
    }

    void printHistory() {
        auto records = history.getRecords();
        for (const auto& record : records) {
            std::cout << "Task " << record.taskId << ": Priority " << record.priority
                      << ", Submitted at " << std::chrono::duration_cast<std::chrono::milliseconds>(record.submitted.time_since_epoch()).count()
                      << ", Started at " << std::chrono::duration_cast<std::chrono::milliseconds>(record.started.time_since_epoch()).count()
                      << ", Finished at " << std::chrono::duration_cast<std::chrono::milliseconds>(record.finished.time_since_epoch()).count()
                      << ", Status " << record.status << std::endl;
        }
    }
};

int main() {
    ThreadPool pool;

    // Simulate adding tasks
    pool.executeTask(5, []() {
        std::this_thread::sleep_for(std::chrono::seconds(1));  // Simulate work
        std::cout << "Task 1 executed\n";
    });

    pool.executeTask(3, []() {
        std::this_thread::sleep_for(std::chrono::seconds(2));  // Simulate work
        std::cout << "Task 2 executed\n";
    });

    pool.printHistory();

    return 0;
}